<?php

namespace frontend\models;

use Yii;
use yii\web\IdentityInterface;

/**
 * This is the model class for table "tba_memberinfo".
 *
 * @property integer $ID
 * @property string $username
 * @property string $Pwd
 * @property string $Email
 * @property string $lName
 * @property double $Balance
 * @property integer $Status
 * @property string $RegDate
 * @property string $ActiveDate
 * @property string $Remark
 * @property string $auth_key
 *
 * @property TbaMembeaddrinfo[] $tbaMembeaddrinfos
 * @property TbaMemberinforecordinfo[] $tbaMemberinforecordinfos
 * @property TmeOrderinfo[] $tmeOrderinfos
 */
class TbaMemberinfo extends \yii\db\ActiveRecord implements IdentityInterface
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'tba_memberinfo';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['username', 'Pwd', 'Email', 'lName'], 'required'],
            [['Balance'], 'number'],
            [['Status'], 'integer'],
            [['RegDate', 'ActiveDate'], 'safe'],
            [['username', 'Pwd', 'lName'], 'string', 'max' => 20],
            [['Email'], 'string', 'max' => 50],
            [['Remark'], 'string', 'max' => 100],
            [['auth_key'], 'string', 'max' => 40]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'username' => 'Username',
            'Pwd' => 'Pwd',
            'Email' => 'Email',
            'lName' => 'L Name',
            'Balance' => 'Balance',
            'Status' => 'Status',
            'RegDate' => 'Reg Date',
            'ActiveDate' => 'Active Date',
            'Remark' => 'Remark',
            'auth_key' => 'Auth Key',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTbaMembeaddrinfos()
    {
        return $this->hasMany(TbaMembeaddrinfo::className(), ['TBa_ID' => 'ID', 'UserName' => 'UserName']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTbaMemberinforecordinfos()
    {
        return $this->hasMany(TbaMemberinforecordinfo::className(), ['ID' => 'ID', 'UserName' => 'UserName']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTmeOrderinfos()
    {
        return $this->hasMany(TmeOrderinfo::className(), ['Uid' => 'ID', 'UserName' => 'UserName']);
    }
    public function beforeSave($insert){
    	if(parent::beforeSave($insert)){
    		if($this->isNewRecord){
    			$this->Balance=0.00;
    			$this->Status=0;
    			$this->RegDate=date('Y-m-d h:i:s',time());
    			
    		}
    		return true;
    	}
    	return false;
    }
    public static function findIdentity($id)
    {
    	return static::findOne(['ID'=>$id]);//键代表AR模型的主键，$id代表传递的参数
    }
    
    
    /**
     * 根据 token 查询身份。
     *
     * @param string $token 被查询的 token
     * @return IdentityInterface|null 通过 token 得到的身份对象
     */
    
    public static function findIdentityByAccessToken($token, $type = null){
    	returnstatic::findOne(['access_token' => $token]);
    }
    
    
    /**
     * @return int|string 当前用户ID
     */
    public function getId()
    {
    	return $this->ID;
    }
    
    /**
     * @return string 当前用户的（cookie）认证密钥
     */
    public function getAuthKey()
    {
    	return $this->auth_key;
    }
    
    /**
     * @param string $authKey
     * @return boolean if auth key is valid for current user
     */
    public function validateAuthKey($authKey)
    {
    	return $this->getAuthKey() === $authKey;
    }
}
